#include "threadPool.h"
#include <ctype.h>
#include <sys/socket.h>
typedef struct trans_train{
    int type;
    int length;
    char contxt[4096];
}trans_train_t;
int recvn(int netfd, void *pstart, int len) {
    int total = 0;
    int ret;
    char *p = (char *)pstart;
    while (total < len) {
        ret = recv(netfd, p + total, len - total, 0);
        if (ret == 0) {
            printf("对方关闭连接\n");
            return -1;
        } else if (ret < 0) {
            perror("recv");
            return -1;
        }
        total += ret;
    }
    return 0;
}
int del(int netfd) {
    int epfd = epoll_create(1);
    epollAdd(epfd,netfd);
    struct epoll_event readyset[1024];
    while(1){
        int readyNum =  epoll_wait(epfd,readyset,1024,-1);
        printf("readyNum = %d\n",readyNum);
        if (readyNum < 0) {
            perror("epoll_wait");
            return -1;
        }
        for(int i = 0;i < readyNum;i++){
            if(readyset[i].data.fd == netfd){
                trans_train_t train;
                bzero(&train, sizeof(train));

                // 先收 type 和 length，再收内容
                recvn(netfd, &train.type, sizeof(int));
                recvn(netfd, &train.length, sizeof(int));
                recvn(netfd, train.contxt, train.length);
                const char* commandList[] = {
                    "null", "cd", "ls", "remove", "pwd", "mkdir", "rmdir", "puts", "gets"
                };

                printf("接收到命令: %s %s\n", commandList[train.type], train.contxt);

            }
        }
    }
    return 0;
}


